home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2005 October / PCWOCT05.iso / Software / FromTheMag / XAMPP 1.4.14 / xampp-win32-1.4.14-installer.exe / xampp / php / pear / Net / Dig.php < prev    next >
PHP Script  |  2004-03-24  |  10KB  |  450 lines

  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2002 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.02 of the PHP license,      |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Author: Colin Viebrock <colin@easyDNS.com>                           |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: Dig.php,v 1.1 2002/04/25 14:57:23 cmv Exp $
  20. //
  21. // A nice friendly OO interface to dig
  22. //
  23.  
  24. require_once('PEAR.php');
  25.  
  26. class Net_Dig extends PEAR
  27. {
  28.     // {{{ Public Properties
  29.     
  30.     /**
  31.      * The address to dig
  32.      *
  33.      * @var string $address
  34.      * @access public
  35.      */
  36.     var $address;
  37.     
  38.     /**
  39.      * The server to use for digging
  40.      *
  41.      * @var string $server
  42.      * @access public
  43.      */
  44.     var $server;
  45.     
  46.     /**
  47.      * The type of DNS records to dig for
  48.      *
  49.      * @var string $query_type
  50.      * @access public
  51.      */
  52.     var $query_type;
  53.  
  54.     /**
  55.      * The last system command executed (for debugging)
  56.      *
  57.      * @var string $cmd
  58.      * @access public
  59.      */
  60.     var $cmd;
  61.  
  62.     /**
  63.      * The raw output of the system command (for debugging)
  64.      *
  65.      * @var string $raw_data
  66.      * @access public
  67.      */
  68.     var $raw_data;
  69.  
  70.     /**
  71.      * The location of the system dig program
  72.      *
  73.      * @var string $dig_prg
  74.      * @access public
  75.      */
  76.     var $dig_prog;
  77.  
  78.     /**
  79.      * The parsed result of the last dig
  80.      *
  81.      * @var string $result
  82.      * @access public
  83.      */
  84.     var $result;
  85.     
  86.     // }}}
  87.  
  88.  
  89.     // {{{ Net_Dig()
  90.     
  91.     /**
  92.      * The Net_Dig constructor
  93.          * Called when a new Net_Dig object is initialized
  94.      *
  95.      * @param string     [$address]  The address to dig (can be set 
  96.      *                               using the $address property as well)
  97.      *
  98.      * @param string     [$server]   The server to dig at (can be set 
  99.      *                               using the $server property as well)
  100.      *
  101.      * @return object Net_Dig   $obj   A new Net_Dig object
  102.      *
  103.      * @access public
  104.      * @author Colin Viebrock <colin@easyDNS.com>
  105.      * @since  PHP 4.0.5
  106.      */
  107.     function Net_Dig($address = false, $server = false )
  108.     {
  109.  
  110.         $this->address = $address;
  111.         $this->server = $server;
  112.         $this->query_type = false;
  113.  
  114.         $this->cmd = '';
  115.         $this->raw_data = '';
  116.  
  117.         $this->result = false;
  118.  
  119.         $this->dig_prog = trim(`which dig`);
  120.         if (!$this->dig_prog) {
  121.             $this = new PEAR_Error("Couldn't find system dig program");
  122.         }
  123.  
  124.     }
  125.  
  126.     // }}}
  127.  
  128.  
  129.  
  130.     // {{{ dig()
  131.     
  132.     /**
  133.      * Does a dig of the given address (or $this->address)
  134.      *
  135.      * @param string           [$address] The address to dig (can be set 
  136.      *                                using the $address property as well)
  137.      *
  138.      * @return object Net_Dig_result    $obj   A new Net_Dig_result object
  139.      *
  140.      * @access public
  141.      * @author Colin Viebrock <colin@easyDNS.com>
  142.      * @since  PHP 4.0.5
  143.      */
  144.     function dig($address=false)
  145.     {
  146.  
  147.         if ($address) {
  148.             $this->address = $address;
  149.         }
  150.  
  151.         if (!$this->address) {
  152.             return new PEAR_Error("No address specified");
  153.         }
  154.  
  155.         if (!$this->_validate_type()) {
  156.             return new PEAR_Error($this->query_type." is an invalid query type");
  157.         }
  158.  
  159.         $cmd = escapeshellcmd(
  160.             sprintf("%s %s %s %s",
  161.                 $this->dig_prog,
  162.                 ($this->server        ? '@'.$this->server : ''),
  163.                 $this->address,
  164.                 ($this->query_type    ? $this->query_type : '' )
  165.             )
  166.         );
  167.  
  168.         $this->cmd = $cmd;
  169.  
  170.  
  171.         $this->raw_data = `$cmd`;
  172.         $this->raw_data = trim(    $this->raw_data );
  173.  
  174.         return $this->_parse_data();
  175.  
  176.     }
  177.     
  178.     // }}}
  179.  
  180.  
  181.     // {{{ _validate_type()
  182.     
  183.     /**
  184.      * Validates the value of $this->query_type
  185.      *
  186.      * @return boolean    $return   True if $this->query_type is a 
  187.      *                                valid dig query, otherwise false
  188.      *
  189.      * @access private
  190.      * @author Colin Viebrock <colin@easyDNS.com>
  191.      * @since  PHP 4.0.5
  192.      */
  193.     function _validate_type()
  194.     {
  195.         $return = true;
  196.         if ($this->query_type) {
  197.             $this->query_type = strtolower($this->query_type);
  198.             switch ($this->query_type) {
  199.                 case 'a':
  200.                 case 'any':
  201.                 case 'mx':
  202.                 case 'ns':
  203.                 case 'soa':
  204.                 case 'hinfo':
  205.                 case 'axfr':
  206.                 case 'txt':
  207.                 break;
  208.                 default:
  209.                 $return = false;
  210.             }
  211.         }
  212.         return $return;
  213.     }
  214.     
  215.     // }}}
  216.  
  217.  
  218.  
  219.     // {{{ _parse_data()
  220.     
  221.     /**
  222.      * Parses the raw data in $this->raw_data
  223.      *
  224.      * @return obj Net_Dig_result  $return   A Net_Dig_result object
  225.      *
  226.      * @access private
  227.      * @author Colin Viebrock <colin@easyDNS.com>
  228.      * @since  PHP 4.0.5
  229.      */
  230.     function _parse_data()
  231.     {
  232.  
  233.         if (!$this->raw_data) {
  234.             return new PEAR_Error("No raw data to parse");
  235.         }
  236.  
  237.         $regex = '/' .
  238.             '^;(.*?)' .
  239.             ';; QUESTION SECTION\:(.*?)' .
  240.             '(;; ANSWER SECTION\:(.*?))?' .
  241.             '(;; AUTHORITY SECTION\:(.*?))?' .
  242.             '(;; ADDITIONAL SECTION\:(.*?))?' .
  243.             '(;;.*)' .
  244.             '/ims';
  245.  
  246.         if (preg_match($regex, $this->raw_data, $matches)) {
  247.  
  248.             $result = new Net_Dig_result;
  249.  
  250.             /* Start parsing the data */
  251.  
  252.  
  253.             /* the header ... */
  254.  
  255.  
  256.             $temp = explode("\n", trim($matches[1]));
  257.             if (preg_match('/DiG (.*?) /i', $temp[0], $m)) {
  258.                 $result->dig_version         = trim($m[1]);
  259.             }
  260.             if (preg_match('/status: (.*?), id: (.*?)$/i', $temp[3], $m)) {
  261.                 $result->status            = trim($m[1]);
  262.                 $result->id            = trim($m[2]);
  263.             }
  264.  
  265.             if (preg_match('/flags: (.*?); query: (.*?), answer: (.*?), authority: (.*?), additional: (.*?)$/i', $temp[4], $m)) {
  266.                 $result->flags            = trim($m[1]);
  267.                 $result->query_count        = (int)trim($m[2]);
  268.                 $result->answer_count        = (int)trim($m[3]);
  269.                 $result->authority_count    = (int)trim($m[4]);
  270.                 $result->additional_count    = (int)trim($m[5]);
  271.             }
  272.  
  273.  
  274.             /* query section */
  275.  
  276.             $line = trim(preg_replace('/^(;*)/', '', trim($matches[2])));
  277.             list($host, $class, $type) = preg_split('/[\s]+/', $line, 3);
  278.             $result->query[] = new Net_Dig_resource($host, false, $class, $type, false);
  279.  
  280.  
  281.             /* answer section */
  282.  
  283.             $temp = trim($matches[4]);
  284.             if ($temp) {
  285.                 $temp = explode("\n", $temp);
  286.                 if (count($temp)) {
  287.                     foreach($temp as $line) {
  288.                         $result->answer[] = $this->_parse_resource($line);
  289.                     }
  290.                 }
  291.             }
  292.  
  293.  
  294.             /* authority section */
  295.  
  296.             $temp = trim($matches[6]);
  297.             if ($temp) {
  298.                 $temp = explode("\n", $temp);
  299.                 if (count($temp)) {
  300.                     foreach($temp as $line) {
  301.                         $result->authority[] = $this->_parse_resource($line);
  302.                     }
  303.                 }
  304.             }
  305.  
  306.  
  307.             /* additional section */
  308.  
  309.             $temp = trim($matches[8]);
  310.             if ($temp) {
  311.                 $temp = explode("\n", $temp);
  312.                 if (count($temp)) {
  313.                     foreach($temp as $line) {
  314.                         $result->additional[] = $this->_parse_resource($line);
  315.                     }
  316.                 }
  317.             }
  318.  
  319.             /* footer */
  320.  
  321.             $temp = explode("\n", trim($matches[9]));
  322.             if (preg_match('/query time: (.*?)$/i', $temp[0], $m)) {
  323.                 $result->query_time    = trim($m[1]);
  324.             }
  325.             if (preg_match('/server: (.*?)#(.*?)\(/i', $temp[1], $m)) {
  326.                 $result->dig_server    = trim($m[1]);
  327.                 $result->dig_port    = trim($m[2]);
  328.             }
  329.  
  330.             /* done */
  331.  
  332.             $result->consistency_check = (
  333.                 (count($result->query) == $result->query_count) &&
  334.                 (count($result->answer) == $result->answer_count) &&
  335.                 (count($result->authority) == $result->authority_count) &&
  336.                 (count($result->additional) == $result->additional_count)
  337.             );
  338.  
  339.             return $result;
  340.  
  341.         }
  342.  
  343.         return new PEAR_Error("Can't parse raw data");
  344.     }
  345.     
  346.     // }}}
  347.  
  348.  
  349.     // {{{ _parse_resource()
  350.     
  351.     /**
  352.      * Parses a resource record line
  353.      *
  354.      * @param string           $line    The line to parse
  355.      *
  356.      * @return obj Net_Dig_resource  $return   A Net_Dig_resource object
  357.      *
  358.      * @access private
  359.      * @author Colin Viebrock <colin@easyDNS.com>
  360.      * @since  PHP 4.0.5
  361.      */
  362.     function _parse_resource($line)
  363.     {
  364.  
  365.         /* trim and remove leading ;, if present */        
  366.  
  367.         $line = trim(preg_replace('/^(;*)/', '', trim($line)));
  368.  
  369.         if ($line) {
  370.             list($host, $ttl, $class, $type, $data) = preg_split('/[\s]+/', $line, 5);
  371.             return new Net_Dig_resource($host, $ttl, $class, $type, $data);
  372.         }
  373.  
  374.         return false;
  375.  
  376.     }
  377.  
  378.     // }}}
  379.  
  380. }
  381.  
  382.  
  383.  
  384. class Net_Dig_result {
  385.  
  386.     // {{{ Public Properties
  387.  
  388.     var $status;
  389.     var $id;
  390.     var $flags;
  391.     var $query_count;
  392.     var $answer_count;
  393.     var $authority_count;
  394.     var $additional_count;
  395.  
  396.     var $dig_version;
  397.     var $dig_server;
  398.     var $dig_port;
  399.  
  400.     var $query;
  401.     var $answer;
  402.     var $authority;
  403.     var $additional;
  404.  
  405.     var $consistency_check;
  406.  
  407.     function Net_Dig_result() {
  408.         $this->status = false;
  409.         $this->id = false;
  410.         $this->flags = false;
  411.         $this->query_count = false;
  412.         $this->answer_count = false;
  413.         $this->authority_count = false;
  414.         $this->additional_count = false;
  415.  
  416.         $this->dig_version = false;
  417.         $this->dig_server = false;
  418.         $this->dig_port = false;
  419.  
  420.         $this->query = array();
  421.         $this->answer = array();
  422.         $this->authority = array();
  423.         $this->additional = array();
  424.  
  425.         $this->consistency_check = false;
  426.  
  427.     }
  428.  
  429. }
  430.  
  431. class Net_Dig_resource {
  432.  
  433.     var $host;
  434.     var $ttl;
  435.     var $class;
  436.     var $type;
  437.     var $data;
  438.  
  439.     function Net_Dig_resource($host=false, $ttl=false, $class=false, $type=false, $data=false) {
  440.         $this->host    = $host;
  441.         $this->ttl    = $ttl;
  442.         $this->class    = $class;
  443.         $this->type    = $type;
  444.         $this->data    = $data;
  445.     }
  446.  
  447. }
  448.  
  449. ?>
  450.